/**
 * 时间管理类
 */
interface TimerObj {
    target: any;
    callback: Function;
    interval: number;
    isPause: boolean;
    progress: number;
}

class TimerMgr {
    private _id = "TimerMgr"
    public static instance: TimerMgr = null
    public static getInstance() {
        if (!TimerMgr.instance) {
            TimerMgr.instance = new TimerMgr()
        }
        return TimerMgr.instance
    }

    init() {
        !cc.director.getScheduler().isScheduled(this.update, this) && cc.director.getScheduler().scheduleUpdate(this, cc.Scheduler.PRIORITY_NON_SYSTEM, false)
    }

    private _timerId = 1;
    private get TimerId(): number {
        return this._timerId++;
    }
    private _timerList: Map<number, TimerObj> = new Map<number, TimerObj>();
    private _addList: Map<number, TimerObj> = new Map<number, TimerObj>();
    private _deleteList: number[] = [];
    private _bGamePause = false

    private update(dt: number) {
        if (this._bGamePause) return

        if (this._deleteList.length > 0) {
            for (let timerId of this._deleteList) {
                if (this._timerList.has(timerId)) {
                    this._timerList.delete(timerId)
                } else if (this._addList.has(timerId)) {
                    this._addList.delete(timerId)
                }
            }
            this._deleteList = []
        }

        if (this._addList.size > 0) {
            this._addList.forEach((value, key) => {
                this._timerList.set(key, value)
            })
            this._addList.clear()
        }

        this._timerList.forEach((value, timerId) => {
            if (!value.isPause) {
                value.progress += dt;
                if (value.progress >= value.interval) {
                    value.progress -= value.interval
                    value.callback.call(value.target, value.interval)
                }
            }
        })
    }


    /**
     * 创建定时器
     * @param target 
     * @param callback 回调
     * @param interval 时间间隔 单位秒
     * @returns ID 唯一标识
     */
    public create(target: any, callback: Function, interval: number): number {
        let timerId = this.TimerId
        let cbs: TimerObj = {
            target: target,
            callback: callback,
            interval: interval,
            isPause: false,
            progress: 0
        }
        this._addList.set(timerId, cbs);
        return timerId;
    }

    /**
     * 暂停计时器
     * @param timerId 
     */
    public pause(timerId: number) {
        if (!timerId) return
        let data = this._timerList.get(timerId)
        if (data) {
            data.isPause = true
        }
    }

    /**
     * 重启计时器
     * @param timerId 
     */
    public resume(timerId: number) {
        if (!timerId) return
        let data = this._timerList.get(timerId)
        if (data) {
            data.isPause = false
        }
    }

    /**
     * 清除指定计时器
     * @param timerId 
     */
    public stop(timerId: number) {
        if (!timerId) return
        let data = this._timerList.get(timerId)
        if (data) {
            data.isPause = true
        }
        this._deleteList.push(timerId)
    }

    /**
     * 定时器是否暂停
     * @param timerId 
     */
    public isPaused(timerId: number): boolean {
        let data = this._timerList.get(timerId)
        if (data) {
            return data.isPause
        }
        return false
    }

    public pauseGame() {
        this._bGamePause = true
    }

    public resumeGame() {
        this._bGamePause = false
    }
}

export let timerMgr = TimerMgr.getInstance()